//
//  OSSUplooadImageUtil.swift
//  GallopFresh
//
//  Created by xiantian on 2023/7/28.
//  Copyright © 2023 鲜恬科技. All rights reserved.
//

import UIKit

struct OSSUploadResult {
    var url: String?
    var error: NSError?
    init(url: String? = nil, error: NSError? = nil) {
        self.url = url
        self.error = error
    }
}

typealias OSSUploadBlock = (UIImage?, OSSUploadResult) -> Void
class OSSUplooadImageUtil {
    var callBack: OSSUploadBlock?
    var image: UIImage?
    func ossUploadImage(image: UIImage, completed: @escaping OSSUploadBlock) {
        self.image = image
        self.callBack = completed
        let imgData = XTCheckCenter.imageData(image)
        self.ossUploadImageStep1(imgData: imgData)
    }
    
    //选图片上传 请求第一步 获取oss签名
   private func ossUploadImageStep1(imgData: Data? = nil){
        if imgData == nil {
            return
        }
        /*let random = String(format: "%d", arc4random_uniform(100))
        let imgName = XTManageCenter.getDateTimeNowByFormater(formater: "yyyyMMddHHmmss") + random + ".JPG"
        var param = NSMutableDictionary() as! [String: String]
        param["type"] = "kuaimaIOSApp"
        param["fileName"] = imgName
       XTRequestCenter.shared.sendRequestMappableTargetObject(target: APIManager.ossUploadImage(imageData: param), objectType: CommondResModel.self, finished: { (response) in
            let result = response as! CommondResModel
            if result.code == "1" && result.data != nil {
                //成功
                let resData = result.data as! [String: Any]
                let url = resData["url"] as! String
                printLog("获取到上传的签名==")
                printLog(url);
                self.ossUploadImageStep2(url: url, imgName: imgName, imgData: imgData!)
            }else{
                //失败
                let result = OSSUploadResult.init(error: NSError.init(domain: "获取签名失败", code: -1))
                self.callBack?(self.image, result)
            }
        }) { (_) in
            let result = OSSUploadResult.init(error: NSError.init(domain: "获取签名失败", code: -1))
            self.callBack?(self.image, result)
        }*/
    }
    
    private func ossUploadImageStep2(url: String, imgName: String, imgData: Data){
        XTRequestCenter.ossUploadImage(imageData: imgData, uploadURL: url, imageName: imgName, success: { (response) in
            let result = response as! NSDictionary
            let code = result["code"] as! String
            if code == "1" {
                //上传成功
                let imgUrl = url.components(separatedBy: "?")[0]
                let result = OSSUploadResult.init(url: imgUrl, error: nil)
                self.callBack?(self.image, result)
            }else{
                //上传失败
                let result = OSSUploadResult.init(error: NSError.init(domain: "上传oss失败", code: -1))
                self.callBack?(self.image, result)
            }
        }) { (error) in
            let result = OSSUploadResult.init(error: NSError.init(domain: "上传oss失败", code: -1))
            self.callBack?(self.image, result)
        }
        
    }

}
